package com.zoweunion.mechanic.controller;


import com.zoweunion.mechanic.dao.FacilitatorPersonnelRelationDao;
import com.zoweunion.mechanic.model.User;
import com.zoweunion.mechanic.plugins.shiro.JWTUtil;
import com.zoweunion.mechanic.service.base.UserService;
import com.zoweunion.mechanic.util.MyException;
import com.zoweunion.mechanic.util.Orgin;
import com.zoweunion.mechanic.util.constants.Constants;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

public class BaseController {

	protected Logger logger = LoggerFactory.getLogger(this.getClass());

	@Autowired
	private UserService userService;
	@Autowired
	private FacilitatorPersonnelRelationDao relationDao;


	public static void logBefore(Logger logger, String interfaceName){
		logger.info("===============START===============");
		logger.info(interfaceName);
	}

	public static void logAfter(Logger logger){
		logger.info("===============END===============");
	}




	public  User getCurrentUser(HttpServletRequest request) throws MyException {
		logger.info("根据请求头获取当前登录用户");
		String authorization = request.getHeader("Authorization");
		//根据请头中是否带有Authorization信息
		if(authorization!=null) {
			String user_account = JWTUtil.getUsername(authorization);
			User currentUser = userService.getUserByAccount(user_account);
			if(currentUser==null) {
				throw new MyException(401,"未登录");
			}
			return currentUser;
		}else {
			throw new MyException(401,"未登录");
		}
	}

	public  void getServerSid(String sid,Map<String, Object> params) throws MyException {
		//判断是否厦工
		if ( sid.equals("b2ee155eed7845809b24e84e54f35060")){
			sid="6bd970ddd3e44e0f8646f951453ce18e";
		}
		//判断是否特殊账号
		params.put("demoType",0);
		if ( sid.equals(Constants.DEMO_ACCOUNT_S_ID[0])){
			params.put("demoType",1);
		}
		List<String> serverString = new ArrayList<>();
		//判断是否是金融平台
		if(params.get("server_type") != null && Integer.valueOf(params.get("server_type").toString()) == 4){
			serverString = relationDao.selectCountByBankingSid(params.get("s_id").toString());
		}else {
			//判断是否是主机厂
			serverString = relationDao.selectCountBySid(sid);
			if (serverString.size() == 0) {
				params.put("isServer", 0); //服务商
			} else {
				params.put("isServer", 1); //主机厂
				//第一步判断是否是主机厂的管理员 不是则查询所管理的服务商 注意：第二步在拦截器对主机厂所有人进行取消拦截处理
				if(params.get("current_logined_user_r_id") != null && !params.get("current_logined_user_r_id").equals(Constants.ROLE_GLY)){
					serverString = relationDao.selectCountByUserId(params.get("current_logined_user_id").toString());
				}
			}
		}
		serverString.add(sid);
		String serverString1 = "'" + StringUtils.join(serverString, "','") + "'";
		params.put("serverString", serverString1);
	}

	/**
	 * 获取当前用户，不校验安全
	 * @param request
	 * @return
	 * @throws MyException
	 */
	public User getCurrentUserNoVerify(HttpServletRequest request){
		logger.info("根据请求头获取当前登录用户");
		String authorization = request.getHeader("Authorization");
		// 根据请头中是否带有Authorization信息
		if (authorization != null) {
			String user_account = JWTUtil.getUsername(authorization);
			return userService.getUserByAccount(user_account);
		} else {
			return null;
		}
	}

	/**
	 * 根据请求头获取设备号
	 * @param request
	 * @return
	 */
	public String getDevice(HttpServletRequest request){
		logger.info("根据请求头获取设备号");
		String device = "";
		if(request.getHeader("Device")!=null){
			device = request.getHeader("Device");
		}
		return device;
	}

}
